//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "telnetwindow.h"
#include "utils.h"


enum cIDS{ cBtnIDSend=12500, cBtnIDCopy };

BEGIN_EVENT_TABLE(CTelnetWindow, wxDialog)
	EVT_BUTTON(cBtnIDSend, CTelnetWindow::OnSend)
	EVT_BUTTON(cBtnIDCopy, CTelnetWindow::OnCopy)
	EVT_UPDATE_UI(cBtnIDSend, CTelnetWindow::OnSendUI)
END_EVENT_TABLE()


CTelnetWindow::CTelnetWindow(bool bSSH, int iError, const wxString& strMsg, const wxString& strLog):
	wxDialog( (wxWindow *)NULL, wxID_ANY, strMsg, wxDefaultPosition, wxDefaultSize,
	wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxSTAY_ON_TOP), m_bSSH(bSSH), m_iError(iError)
{
	wxBoxSizer* szMain = new wxBoxSizer(wxVERTICAL);	

	m_pedtLog = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY|wxTE_MULTILINE);

	wxFont  font(10, wxFONTFAMILY_TELETYPE,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL|wxFONTFLAG_ANTIALIASED);
	m_pedtLog->SetDefaultStyle(wxTextAttr(*wxGREEN, *wxBLACK, font));
	m_pedtLog->AppendText(strLog);


	szMain->Add(m_pedtLog,1,wxEXPAND|wxALL|wxALIGN_CENTER, 10);

	//Form
	wxFlexGridSizer* fgs = new wxFlexGridSizer(3, 2, 10, 5);

	wxStaticText* plbManuf = new wxStaticText(this, -1, _("Router manufacturer:"));
	wxStaticText* plbModel = new wxStaticText(this, -1, _("Router model:"));

	m_pedtRouterManufacturer= new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize);
	m_pedtRouterModel= new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize);

	fgs->Add(plbManuf);
	fgs->Add(m_pedtRouterManufacturer);

	fgs->Add(plbModel);
	fgs->Add(m_pedtRouterModel);
	fgs->SetFlexibleDirection(wxHORIZONTAL);

	fgs->AddGrowableCol(1, 1);
	szMain->Add(fgs, 0, wxALIGN_LEFT|wxLEFT|wxEXPAND, 10 );
		
	//Buttons
	wxSizer* sz = CreateStdDialogButtonSizer(wxOK);

	wxButton* pSendLog = new wxButton(this, cBtnIDSend, _("Send Log to developer") );
	sz->Add(pSendLog, 0, wxLEFT, 10);

	wxButton* pCopy = new wxButton(this, cBtnIDCopy, _("Copy") );
	sz->Add(pCopy, 0, wxLEFT, 10);

	szMain->Add(sz,0, wxALIGN_CENTER|wxALL, 10);

	SetSizer(szMain);
	szMain->Fit(this);
}

CTelnetWindow::~CTelnetWindow(void)
{
}

void CTelnetWindow::OnSend(wxCommandEvent& event)
{
	wxString strURL = wxT("mailto:support@routercommander.com?subject=router connection problem.");

	strURL+=wxT(" Manufacturer:");
	strURL+=m_pedtRouterManufacturer->GetValue();

	strURL+=wxT(" Model:");
	strURL+=m_pedtRouterModel->GetValue();

	strURL+=wxT(" Type:");
	strURL+=m_bSSH?wxT("SSH"):wxT("Telnet");

	strURL+=wxT(" Error:");
	strURL+=wxString::Format(wxT("%d"), m_iError);

	strURL+=wxT("&body=");

	wxString strLog = m_pedtLog->GetValue();

	strURL+=URLEncode(strLog);
	wxLaunchDefaultBrowser(strURL);
}

void CTelnetWindow::OnCopy(wxCommandEvent& event)
{
	m_pedtLog->SelectAll();
	m_pedtLog->Copy();
}

void CTelnetWindow::OnSendUI(wxUpdateUIEvent& event)
{
	bool bValid = !m_pedtRouterModel->GetValue().empty() &&
		!m_pedtRouterManufacturer->GetValue().empty();

	event.Enable( bValid );
}